/**
 * 商城主逻辑
 *
 * douzy 2016-07-11 add
 */

function ShopProduct() {
    //url
    this.ajaxUrl = {
        banner: "/product/main.do", //含
        cate: "/catalog/catalogListByParentAndType.do"
    }

    //变量
    this.variable = {
        $bannerPannel: $("#index-slides"),
        $bannerLi: $("#banner-template"),
        $cataUL: $("#cataUl"),
        $cateLi: $("#cata-template"),
        $hotSaleUL: $("#hotSaleUL"),
        $hotSaleLi: $("#hotSale-template"),
        $newProLi: $("#newPro-template"),
        $newProPannel: $("#newProPannel")
    }

    this.common = new common()   //通用
}

//初始化
ShopProduct.prototype.init = function () {
    var self = this;
    self.getBanner();
    self.getCataLog();
}
//Banner
ShopProduct.prototype.getBanner = function () {
    var self = this;
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.banner,
        dataType: "json",
        success: function (data) {
            var $bannerObj = self.common.evalJson(data);
            if (!!$bannerObj) {
                if ($bannerObj.status == "success") {

                    var myTemplate = Handlebars.compile(self.variable.$bannerLi.html());
                    self.variable.$bannerPannel.html(myTemplate(data));


                    //热销
                    var myTemplate1 = Handlebars.compile(self.variable.$hotSaleLi.html());
                    self.variable.$hotSaleUL.html(myTemplate1(data));

                    //新品
                    var myTemplate2 = Handlebars.compile(self.variable.$newProLi.html());
                    self.variable.$newProPannel.html(myTemplate2(data));
                    //
                    $('.am-slider').flexslider();
                }
            }
        }
    })
}
//分类
ShopProduct.prototype.getCataLog = function () {
    var self = this;
    $.ajax({
        type: "GET",
        url: self.ajaxUrl.cate,
        dataType: "json",
        success: function (data) {
            var $bannerObj = self.common.evalJson(data);
            if (!!$bannerObj) {
                if ($bannerObj.status == "success") {
                    var myTemplate = Handlebars.compile(self.variable.$cateLi.html());
                    self.variable.$cataUL.html(myTemplate(data));
                }
            }
        }
    })
}

/**
 * pager load
 */
$(function () {
    new ShopProduct().init();
});